package com.gdqy.FCS.web;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gdqy.FCS.common.api.FastResponse;
import com.gdqy.FCS.common.api.ListResponse;
import com.gdqy.FCS.common.mvc.Args;
import com.gdqy.FCS.entity.Design;
import com.gdqy.FCS.entity.Special;
import com.gdqy.FCS.service.DesignService;
import com.gdqy.FCS.service.SpecialService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("special")
public class SpecialController {
	@Autowired
	private DesignService designService;
	@Autowired
	private SpecialService specialService;
	//获取专辑里面的所有作品
	
	/**
	 * 
	 *作者：张伟键
	 *方法描述：
	 * @param special_code 专辑id
	 * @return 
	 */
	@RequestMapping("getDesignBySpecial_code")
	public ModelAndView getDesignBySpecial_code(
			@RequestParam(value="special_code",required = true) int special_code){
		ModelAndView modelAndView = new ModelAndView();
		//special_id 非空验证
		if(special_code == 0){
			return null;
		}
			List<Design> designs = designService.selectBySpecial_code(special_code);
		modelAndView.addObject("designs", designs);
		modelAndView.setViewName("designer/designs");
		return modelAndView;
		
	}
	
	//处理页面跳转,获取专辑
	
	/**
	 * 
	 *作者：张伟键
	 *方法描述：
	 * @param designer_id 设计师id
	 * @param args
	 * @param pageindex 当前页面
	 * @param pagesize 页面数据容量
	 * @return
	 */
		@RequestMapping("deal_special")
		public ModelAndView deal_special(@RequestParam(value = "designer_id" ,required = true) int designer_id,
				Args args,int pageindex, int pagesize){
			ModelAndView modelAndView = new ModelAndView();
			//获取现有专辑
			//分页返回
			PageHelper.startPage(pageindex, pagesize);
			//获取收藏作品
			List<Special> specials = specialService.selectByDesigner_id(designer_id);
			PageInfo<Special> page = new PageInfo<Special>(specials);
			//分页对象
			ListResponse<Special> json= FastResponse._().errCode(0).subCode(0).errMsg("获取购买需求成功")
					.args(args.getReqeustargs()).pageIndex(page.getPageNum())
					.pageSize(page.getSize()).total((int)page.getTotal()).pageCount(page.getPages())
					.hasNext(page.isHasNextPage()).list(specials);
			
			modelAndView.addObject("json", json);
			modelAndView.setViewName("designer/special");
			return modelAndView;
		}
		
		//增加专辑
		
		/**
		 * 
		 *作者：张伟键
		 *方法描述：
		 * @param special 专辑
		 * @return
		 */
		@RequestMapping("addSpecial")
		public @ResponseBody Object addSpecial(Special special){
			String msg = "";
			//special非null
			if(special != null ){
				int designer_id = special.getDesigner_id();
				String name = special.getName();
				//重要字段非空
				if(designer_id != 0 && name != "" && name!=null){
					if(specialService.selectByDesigner_id_and_name(special) == null){
						specialService.insert(special);
						msg = "success";
					}else{
						msg = "fail";
					}
				}else{
					msg = "fail";
				}
			}else{
				msg = "fail";
			}
			return FastResponse._().obj(msg);
			
		}
		
		//删除专辑-----测试完毕
		@RequestMapping("deleteSpecial")
		public @ResponseBody Object deleteSpecial(int id,HttpSession session){
			
			//获取session中的designer_id
			int session_designer_id = (int) session.getAttribute("designer_id");
			
			String msg = "fail";
			//专辑信息
			Special special = specialService.selectById(id);
			//判断专辑是否为该设计师所有
			if(session_designer_id == special.getDesigner_id()){
				/*判断专辑里面有无作品，有则转移，无则不做处理*/
				
				if(special.getDesign_count() == 0){
					if(specialService.delete(special)>0){
						msg = "success";
					}else{
						msg = "fail";
					}
					
					return FastResponse._().obj(msg);
				}else{

					//查询该用户的默认专辑，并获取id
					Special  defaultSpecial = new Special();
					defaultSpecial.setDesigner_id(special.getDesigner_id());
					defaultSpecial.setName("默认专辑");
					defaultSpecial  = specialService.selectByDesigner_id_and_name(defaultSpecial);
					
					//取默认专辑的id
					int defaultSpecial_code = defaultSpecial.getId();
					
					
					//原专辑作品转移
					designService.transferSpecial(defaultSpecial_code, special.getId());
					
					//真正的删除操作
					if(specialService.delete(special)>0){
						msg = "success";
					}else{
						msg = "fail";
					}
				}
			}
			return FastResponse._().obj(msg);
		}
		
		//修改专辑-----测试完毕
		@RequestMapping("updateSpecial")
		public @ResponseBody Object updateSpecial(Special special){
			
			String msg = "";
			
				if(specialService.selectById(special.getId()) != null){
					specialService.update(special);
					msg="修改成功";
				}else{
					msg = "不存在改专辑，无法修改";
				}
			
			return FastResponse._().obj(msg);
			
		}
		
		//显示专辑里面的作品
		@RequestMapping("special_detail")
		public ModelAndView special_detail(@RequestParam(value="id",required = true) int id,
				String type,Args args,int pageindex,int  pagesize){
			String path = "";
			path ="/designer/special_detail";
			if(type!=null) {
				if(type.equals("common")){
					path="designer/show_design";
				}
			}
				
			
			ModelAndView modelAndView = new ModelAndView();
			
			//获取专辑里面的作品并且分页
			PageHelper.startPage(pageindex, pagesize);
			List<Design> designs = designService.selectBySpecial_code(id);
			if(designs==null || designs.size()==0){
				String msg="该专辑并没有作品，赶紧添加吧！";
				modelAndView.addObject("msg", msg);
				modelAndView.setViewName("designer/addDesign");
			}
			PageInfo<Design> page = new PageInfo<Design>(designs);
			Object data= FastResponse._().errCode(0).subCode(0).errMsg("获取专辑详细作品成功")
					.args(args.getReqeustargs()).pageIndex(page.getPageNum())
					.pageSize(page.getSize()).total((int)page.getTotal()).pageCount(page.getPages())
					.hasNext(page.isHasNextPage()).list(designs);
			
			modelAndView.addObject("json", data);
			modelAndView.setViewName(path);
			return modelAndView;
			
		}
		//修改专辑名称
		@RequestMapping("change_special_name")
		public @ResponseBody void change_special_name(Special special){
			specialService.update(special);
		}
}
